package utils;

import user.AdminUser;
import user.ProxyUser;
import user.User;
import user.factory.AdminUserFactory;
import user.factory.IUserFactory;
import user.factory.NormalUser;

import java.lang.reflect.Proxy;
import java.util.Scanner;

public class LibrarySystem {
    public static void main(String[] args) {

        //工厂模式
        IUserFactory adminUser = new AdminUserFactory();
        User admin = adminUser.createUser("刘备",1);

        IUserFactory normalUser = new NormalUser();
        User normal1 = normalUser.createUser("关羽",2);
        User normal2 = normalUser.createUser("张飞",2);

        //代理模式
        ProxyUser proxyUserAdmin = new ProxyUser(admin);
        ProxyUser proxyUserNormal1 = new ProxyUser(normal1);
        ProxyUser proxyUserNormal2 = new ProxyUser(normal2);

        LibrarySystem librarySystem = new LibrarySystem();

        //selectProxyRole是用于判断角色

        ProxyUser currentUser = librarySystem.selectProxyRole(proxyUserAdmin,proxyUserNormal1,proxyUserNormal2);

        while(true){

            int choice = currentUser.display();
            //handleOperation是用于呈现不同角色的菜单
            currentUser.handleOperation(choice);
        }

    }

    private static ProxyUser selectProxyRole(ProxyUser proxyUserAdmin, ProxyUser proxyUserNormal1, ProxyUser proxyUserNormal2) {
        System.out.println("选择⻆⾊进⾏登录：");
        System.out.println("1.管理员\n2.普通⽤⼾(关⽻)\n3.普通⽤⼾(张⻜)\n4.退出系统");
        ProxyUser currentUser = null;
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        switch (choice) {
            case 1:
                currentUser = proxyUserAdmin;
                break;
            case 2:
                currentUser = proxyUserNormal1;
                break;
            case 3:
                currentUser = proxyUserNormal2;
                break;
            case 4:
                System.exit(0);
                System.out.println("系统已退出..");
                break;
            default:
                break;
        }
        return currentUser;

    }
}
